import { Component, OnInit } from '@angular/core';
import { SearchService } from './search.service';
import { NavigationEnd, Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { Pokemon } from '../pokemon/pockemon';
import { Params } from '@angular/router';
@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
  h_p_page: number | undefined;
  p_page: number | undefined;
  h_l_page: number | undefined;
  l_page: number | undefined;

  toal_page: number | undefined;
  pages: Array<number> | undefined;
  pockemons: Pokemon[] = [];
  page: number = 1;
  stype: string = ""
  wd: string = ""
  constructor(private searchService: SearchService, private router: Router, private route: ActivatedRoute) {
    router.events.subscribe(val => {

      // console.log(val)
      if (val instanceof NavigationEnd) {
        // console.log(111);
        this.wd = this.route.snapshot.queryParams['wd']
        this.stype = this.route.snapshot.queryParams['stype']
        this.getSearch(this.stype, this.wd);
        // this.router.navigate(['/search'], { queryParams: { stype: this.stype, wd:  this.wd, page: 1 } });
      }
    })
  }



  ngOnInit(): void {

    this.wd = this.route.snapshot.queryParams['wd']
    this.stype = this.route.snapshot.queryParams['stype']
    
    this.getSearch(this.stype, this.wd); //调用
  }

  public getSearch(stype: string, wd: string, page: number = 1) {
    
    this.page = page
    this.searchService.getSearch(stype, wd, page).subscribe(
      data => {
        if (data["stats"] == "ok") {
          this.pockemons = data['datas']
          this.h_p_page = data['h_p_page']
          this.p_page = data['p_page']
          this.h_l_page = data['h_l_page']
          this.l_page = data['l_page']
          this.toal_page = data['toal_page']
          this.pages = data['pages']
          this.page = data['page']
        }
        else {
          alert("获取数据失败")
        }
      }
    )
  }


}
